package com.baichang.taoshunda.dao.impl;

import com.baichang.framework.model.Page;
import org.springframework.stereotype.Repository;

import com.baichang.framework.dao.impl.BaseDaoImpl;
import com.baichang.taoshunda.dao.CarouselDao;
import com.baichang.taoshunda.entity.Carousel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class CarouselDaoImpl extends BaseDaoImpl<Carousel> implements CarouselDao {
    /**
     * 根据城市获取首页轮播图
     *
     * @param areaId 城市编号
     * @param type
     * @param lat
     * @param lng    @return
     */
    @Override
    public List<Map<String, Object>> getCarouselByCity(Integer areaId, Integer type, String lat, String lng) {
        String sql = "SELECT *,link AS companyId,ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN(((" + lat + ") * PI() / 180 - tc.lat * PI() / 180) / 2),2" +
                "  ) + COS(40.0497810000 * PI() / 180) * COS(tc.lat * PI() / 180) * POW( " +
                "   SIN(((" + lng + ") * PI() / 180 - tc.lng * PI() / 180) / 2),2 " +
                "   ))) * 1000) AS juli FROM t_carousel tc INNER JOIN t_bussiness tb ON tb.id=tc.link " +
                " WHERE (tc.city_id=? or tb.id=91) AND tc.status=1 AND tc.pass=2 AND tb.state=1  AND DATE_FORMAT(tc.end_time,'%Y-%m-%d')>= DATE_FORMAT(NOW(),'%Y-%m-%d') AND tc.link_type=?";
        sql += " ORDER BY juli ASC LIMIT 10 ";
        return queryForMapList(sql, areaId, type);
    }

    /**
     * 修改轮播图状态
     *
     * @param carouselId
     * @return
     */
    @Override
    public int updateStatus(Integer carouselId) {
        String sql = "UPDATE {table} SET status=0 WHERE id=?";
        return update(sql, carouselId);
    }

    /**
     * @param id
     * @param orderNum
     * @return
     */
    @Override
    public int updateOrderNum(Integer id, Integer orderNum) {
        String sql = "UPDATE {table} SET order_num=? WHERE id=?";
        return update(sql, orderNum, id);
    }

    /**
     * 获取商家轮播图信息
     *
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> queryBusInfo(Integer id) {
        String sql = "SELECT * FROM {table} WHERE link=? AND status=1";
        return queryForMap(sql, id);
    }

    /**
     * 修改轮播图经纬度
     *
     * @param id
     * @param lat
     * @param lng    @return
     * @param cityId
     */
    @Override
    public int updateCarouselInfo(Integer id, String lat, String lng, Integer cityId) {
        String sql = "UPDATE {table} SET lat=?,lng=?,city_id=? WHERE link=?";
        return update(sql, lat, lng, cityId, id);
    }

    /**
     * @param businessName
     * @param type
     * @param nowPage
     * @param pageSize     @return
     */
    @Override
    public Page queryAuditingCarosuel(String businessName, Integer type, int nowPage, int pageSize) {
        List list = new ArrayList();
        String sql = " SELECT tc.*,tt.name AS cityName,ty.name AS areaName,tb.buss_name AS busName,tb.account,th.payment_time AS patTime,th.payment_money AS payMoney " +
                " FROM t_carousel tc" +
                " INNER JOIN t_bussiness tb ON tb.id=tc.link AND tb.state=1" +
                " INNER JOIN t_city ty ON ty.id=tb.area_id " +
                " INNER JOIN t_city tt ON tt.id=tb.city_id" +
                " INNER JOIN t_company_panyment_history th ON th.carousel_id=tc.id " +
                " WHERE tc.status=1 AND tc.pass IN (1)";
        if (!businessName.equals("")) {
            sql += " AND tb.buss_name LIKE ?";
            list.add("%" + businessName + "%");
        }
        if (type > 0) {
            sql += "AND tc.link_type =?";
            list.add(type);
        }
        sql += " ORDER BY tc.created DESC";
        return pageForMapList(sql, nowPage, pageSize, list.toArray());
    }

    /**
     * 轮播图上传图片
     *
     * @param id
     * @param picture
     * @param pass
     * @param startTime
     * @param endTime   @return
     */
    @Override
    public int addPicture(Integer id, String picture, Integer pass, String startTime, String endTime) {
        String sql = "UPDATE {table} SET pic_path=?,pass=?,start_time=?,end_time=? WHERE id=?";
        return update(sql, picture, pass, startTime, endTime, id);
    }

    /**
     * 审核拒绝
     *
     * @param id
     * @param pass
     * @return
     */
    @Override
    public int updatePass(Integer id, Integer pass) {
        String sql = "UPDATE {table} SET pass=? WHERE id=?";
        return update(sql, pass, id);
    }

    /**
     * 查询最新创建的数据id
     *
     * @return
     */
    @Override
    public Map<String, Object> queryCarouselId() {
        String sql = "SELECT * FROM {table} ORDER BY created DESC LIMIT 1";
        return queryForMap(sql);
    }

    /**
     * 查询我的轮播图
     *
     * @param
     * @param
     * @return
     */
    @Override
    public Map<String, Object> queryBusCarouselDetails(String id) {
        String sql = "SELECT * FROM {table} WHERE id=?";
        return queryForMap(sql, id);
    }

    /**
     * @param busId
     * @param type
     * @return
     */
    @Override
    public Map<String, Object> queryBusCarousel(Integer busId, int type) {
        String sql = "SELECT * FROM {table} WHERE link=? AND status=1 AND pass=1 AND link_type=?";
        return queryForMap(sql, busId, type);
    }

    /**
     * 查询同意和拒绝的轮播图
     *
     * @param name
     * @param cityId
     * @param nowPage
     * @param pageSize @return
     */
    @Override
    public Page queryCarousel(String name, Integer cityId, Integer type, int nowPage, int pageSize) {
        List list = new ArrayList();
        String sql = "SELECT tc.*,ty.name AS cityName,tb.buss_name AS busName,th.payment_time AS patTime,th.payment_money AS payMoney FROM t_carousel tc INNER JOIN t_city ty ON ty.id=tc.city_id " +
                " INNER JOIN t_bussiness tb ON tb.id=tc.link " +
                "INNER JOIN t_company_panyment_history th ON th.carousel_id=tc.id " +
                " WHERE tc.status=1 AND tc.pass IN (2,3) AND DATE_FORMAT(tc.end_time,'%Y-%m-%d')>= DATE_FORMAT(NOW(),'%Y-%m-%d') ";
        if (!name.equals("")) {
            sql += " AND tb.buss_name LIKE ?";
            list.add("%" + name + "%");
        }
        if (cityId > 0) {
            sql += " AND tc.city_id=?";
            list.add(cityId);
        }
        if (type > 0) {
            sql += "AND tc.link_type =?";
            list.add(type);
        }
        sql += " ORDER BY tc.created DESC";
        return pageForMapList(sql, nowPage, pageSize, list.toArray());
    }

    /**
     * 更换图片
     *
     * @param id
     * @param picture
     * @return
     */
    @Override
    public int replacePicture(Integer id, String picture) {
        String sql = "UPDATE {table} SET pic_path=? WHERE id=?";
        return update(sql, picture, id);
    }

    /**
     * 查询当前商家的轮播图信息
     *
     * @param busId
     * @return
     */
    @Override
    public Map<String, Object> getCarouselById(Integer busId) {
        String sql = "SELECT * FROM {table} WHERE link=? AND status=1 ";
        return queryForMap(sql, busId);
    }

    /**
     * @param busId
     * @param code
     * @return
     */
    @Override
    public int updateAuditingStatus(Integer busId, Integer code) {
        String sql = "UPDATE {table} SET pass=? WHERE link=?";
        return update(sql, code, busId);
    }

    /**
     * 当前轮播图时间
     *
     * @param busId
     * @param type
     * @return
     */
    @Override
    public List<Map<String, Object>> queryCarouselTime(Integer busId, Integer type) {
        String sql = "SELECT id,IFNULL(end_time,0) AS endTime FROM {table} WHERE link=? AND status=1 AND link_type=?";
        return queryForMapList(sql, busId, type);
    }

    /**
     * 查询全部的过往记录
     *
     * @param busId
     * @param type
     * @return
     */
    @Override
    public List<Map<String, Object>> queryAllHistory(Integer busId, Integer type) {
        //AND DATE_FORMAT(end_time,'%Y-%m-%d')<= DATE_FORMAT(NOW(),'%Y-%m-%d')
        String sql = "SELECT * FROM t_carousel WHERE link= ?  AND link_type=? AND status=1 AND DATE_FORMAT(end_time,'%Y-%m-%d')<= DATE_FORMAT(NOW(),'%Y-%m-%d')";
        return queryForMapList(sql, busId, type);
    }
}